#!/system/bin/sh
# Project WIPE https://github.com/yc9559/cpufreq-interactive-opt
# Author: yc9559
# Generated at: Thu May 10 14:52:06 2018

# $1:value $2:file path
function set_value() {
	if [ -f $2 ]; then
		chown 0.0 $2
		chmod 0666 $2
		echo $1 > $2
		chmod 0444 $2
	fi
}

# $1:cpu0 $2:timer_rate $3:value
function set_param() {
	echo $3 > /sys/devices/system/cpu/cpufreq/interactive/$2
}

# $1:io-scheduler $2:block-path
function set_io() {
	if [ -f $2/queue/scheduler ]; then
		if [ `grep -c $1 $2/queue/scheduler` = 1 ]; then
			echo $1 > $2/queue/scheduler
			echo 512 > $2/queue/read_ahead_kb
			set_value 0 $2/queue/iostats
			set_value 256 $2/queue/nr_requests
			set_value 0 $2/queue/iosched/slice_idle
		fi
	fi
}

action=$1
setenforce 0

# RunOnce
if [ ! -f /dev/project_wipe_runonce ]; then
	# make sure that sysfs is RW
	mount -o remount,rw sysfs /sys
fi

chown 0.0 /sys/devices/system/cpu/cpufreq/interactive/*
chmod 0666 /sys/devices/system/cpu/cpufreq/interactive/*

# RunOnce
if [ ! -f /dev/project_wipe_runonce ]; then
	# set flag
	touch /dev/project_wipe_runonce

	# CORE CONTROL
	set_value 50 /proc/hps/down_threshold

	set_value 2-3 /dev/cpuset/background/cpus
	set_value 2-3 /dev/cpuset/system-background/cpus
	set_value 0-2,4-7,8 /dev/cpuset/foreground/cpus
	set_value 0-2,4-7,8 /dev/cpuset/top-app/cpus

	set_io cfq /sys/block/mmcblk0
	set_io cfq /sys/block/sda

	# shared interactive parameters
	set_param cpu0 timer_rate 20000
	set_param cpu0 timer_slack 180000
	set_param cpu0 io_is_busy 0
fi

if [ "$action" = "powersave" ]; then
	set_value 90 /proc/hps/up_threshold
	set_value "2 2 0" /proc/hps/num_base_perf_serv
	set_param cpu0 boost 0
	set_param cpu0 above_hispeed_delay "18000 1280000:58000"
	set_param cpu0 hispeed_freq 1180000
	set_param cpu0 go_hispeed_load 99
	set_param cpu0 boostpulse_duration 58000
	set_param cpu0 target_loads "80 380000:22 480000:28 580000:23 680000:40 780000:54 880000:86 980000:65 1080000:88 1380000:68 1480000:96"
	set_param cpu0 min_sample_time 18000
fi

if [ "$action" = "balance" ]; then
	set_value 80 /proc/hps/up_threshold
	set_value "3 2 0" /proc/hps/num_base_perf_serv
	set_param cpu0 boost 0
	set_param cpu0 above_hispeed_delay "18000 1280000:58000 1380000:38000 1480000:58000"
	set_param cpu0 hispeed_freq 1180000
	set_param cpu0 go_hispeed_load 94
	set_param cpu0 boostpulse_duration 58000
	set_param cpu0 target_loads "80 380000:21 680000:36 880000:84 980000:43 1080000:90 1180000:95 1280000:87 1380000:68 1480000:98"
	set_param cpu0 min_sample_time 18000
fi

if [ "$action" = "performance" ]; then
	set_value 75 /proc/hps/up_threshold
	set_value "3 3 1" /proc/hps/num_base_perf_serv
	set_param cpu0 boost 0
	set_param cpu0 above_hispeed_delay "18000 1280000:58000 1380000:38000 1480000:58000"
	set_param cpu0 hispeed_freq 1180000
	set_param cpu0 go_hispeed_load 94
	set_param cpu0 boostpulse_duration 58000
	set_param cpu0 target_loads "80 380000:22 680000:36 880000:41 980000:77 1080000:91 1280000:83 1380000:50 1480000:98"
	set_param cpu0 min_sample_time 38000
fi

if [ "$action" = "fast" ]; then
	set_value 70 /proc/hps/up_threshold
	set_value "3 3 1" /proc/hps/num_base_perf_serv
	set_param cpu0 boost 1
	set_param cpu0 above_hispeed_delay "18000 1780000:38000"
	set_param cpu0 hispeed_freq 1480000
	set_param cpu0 target_loads "90 1780000:95"
fi

chmod 0444 /sys/devices/system/cpu/cpufreq/interactive/*

exit 0